package com.flower.hooc.app.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.flower.hooc.bean.pojo.vo.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@CrossOrigin
@RestController
@RequestMapping("/hooc/messages")
@Api(tags = "短信发送接口")
public class MessagesController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * @param phone
     * @param req
     */
    @GetMapping("/getMessage")
    @ApiOperation("根据手机号发送短信")
    @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号"
            , required = true, dataType = "String")
    public RespResult getMessage(String phone, HttpServletRequest req) {

        /* *//*获取phone*//*
        String phone = req.getParameter("phone");
*/
        String phoneString = "" + phone;
        // Map<String, Object> map = new HashMap<>();
        System.out.println(phone);
        Map codeMap = new HashMap();
        int n = 6;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        String codeString = code.toString();
        codeMap.put("code", codeString);

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GGnaDaEZPjGKTCvoJex", "ugHTzBJOZN7Yya5aS3zCM15UTVVmoz");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneString); // 手机号码
        request.putQueryParameter("SignName", "Hooc在线教育平台");   // 签名
        request.putQueryParameter("TemplateCode", "SMS_200714000");    // 验证码模板
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(codeMap));
        System.out.println("code");
        System.out.println(phone + "phone is =");
        /*将验证码存入session*/
        req.getSession().setAttribute("verification", codeString);
        HttpSession session = req.getSession();
        session.setMaxInactiveInterval(300);

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        RespResult respResult = new RespResult();
        respResult.setData("successful");
        return respResult;
    }

    @GetMapping("/checkMessage")
    @ApiOperation("验证code")
    @ApiImplicitParam(paramType = "query", name = "phoneCode", value = "验证码"
            , required = true, dataType = "String")
    public RespResult checkMessage(String phoneCode, HttpServletRequest request) {
        System.out.println("inputcode is" + phoneCode);
        String verification = (String) request.getSession().getAttribute("verification");
        System.out.println("sendmassages is" + verification);
        if (phoneCode == null || !phoneCode.equalsIgnoreCase(verification)) {
            return returnRespWithFailed("验证码错误");
        }
        RespResult respResult = new RespResult();
        respResult.setData("successful");
        return respResult;
    }
}
